package com.zxy.controller;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import com.zxy.config.OSSConfig;
import com.zxy.model.FileInfo;
import com.zxy.model.UploadInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayInputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
@RequestMapping("/api/upload2")
public class UpLoad2Controller {

    @Autowired
    private OSSConfig ossConfig;

    @PostMapping
    public  String Upload(@RequestBody UploadInfo uploadInfo){
        //获取相关配置
        String bucketName = ossConfig.getBucketName();
        String endPoint = ossConfig.getEndPoint();
        String accessKeyId = ossConfig.getAccessKeyId();
        String accessKeySecret = ossConfig.getAccessKeySecret();

        //创建OSS对象
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
        //设置名称
        String timestamp = String.valueOf(System.currentTimeMillis());
        String   name = uploadInfo.getName();
        name = timestamp + "_"+ name;
        //还原base64成2进制文件
        String  base64 = uploadInfo.getBase64();
        String[] strArray = StrUtil.splitToArray(base64, "base64,");
        byte[] bytes = Base64.decode(strArray[1]); //去把第二节转换回二进制数据

        PutObjectResult putObjectResult = ossClient.putObject(bucketName, "wms-images/" + name, new ByteArrayInputStream(bytes));

        return "https://"+bucketName+"."+endPoint+"/"+name;


    }
    private static List<String> imgExts= CollUtil.newArrayList("jpg","png","gif","jpeg");
    @GetMapping
    public  List<FileInfo>  getFiles(){
        //获取相关配置
        String bucketName = ossConfig.getBucketName();
        String endPoint = ossConfig.getEndPoint();
        String accessKeyId = ossConfig.getAccessKeyId();
        String accessKeySecret = ossConfig.getAccessKeySecret();
        String prefix = "wms-images/";

        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);

        try {
            List<FileInfo> fileInfos = new ArrayList<>();

            // 设置URL过期时间，默认为1小时
            Date expiration = new Date(System.currentTimeMillis() + 3600 * 10000);

            // 获取文件列表
            Iterable<com.aliyun.oss.model.OSSObjectSummary> ossObjectSummaries =
                    ossClient.listObjects(bucketName, prefix).getObjectSummaries();

            // 遍历文件列表，生成每个图片的URL，并创建FileInfo对象保存图片信息
            for (com.aliyun.oss.model.OSSObjectSummary objectSummary : ossObjectSummaries) {
                String objectName = objectSummary.getKey();
                URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
                FileInfo fileInfo = new FileInfo(objectName, url.toString());
                fileInfos.add(fileInfo);
            }

            // 将fileInfos列表作为响应返回
            return fileInfos;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭OSSClient
            ossClient.shutdown();
        }

        // 出错或返回空数组时返回null
        return null;

    }
}